package com.huixue.cn.ztej.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.util.StringUtil;
import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.bean.TClassInfo;
import com.huixue.cn.ztej.bean.TOrderInfo;
import com.huixue.cn.ztej.bean.TTudentInfo;
import com.huixue.cn.ztej.persistence.Domain;
import com.huixue.cn.ztej.service.TTudentService;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;
import com.huixue.cn.ztej.utils.framwork.PageParameter;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.Model;

@Domain(TTudentInfo.class)
@Controller
@RequestMapping(value = "/sys/tstudent")
public class TStudentController {
	
	@Autowired(required = true)
	private TTudentService sTudentService;

	@RequestMapping(value = "/list")
	public String list(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = PageParameter.DEFAULT_PAGE_SIZE+"") Integer rows,
			TTudentInfo searchStudentForm,Model model,HttpServletRequest request) {
		Map<String,Object> searchParams = new HashMap<String,Object>();
		if(StringUtil.isNotEmpty(searchStudentForm.getName())){
			searchParams.put("name", searchStudentForm.getName());
		}
		if(searchStudentForm.getGradeId()!=null){
			searchParams.put("gradeId", searchStudentForm.getGradeId());
		}
		
		Results results = sTudentService.searchByPage(searchParams,page,rows);
		model.addAttribute("res", results);
		model.addAttribute("searchStudentForm", searchStudentForm);
		return "/student/student_list";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toAdd")
	public String toAdd(Model model) {
		return "/student/student_add";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toUpdate/{id}")
	public String toUpate(@PathVariable ("id")Integer id,Model model,HttpServletRequest request) {
		TTudentInfo info = sTudentService.getInfo(id);
		model.addAttribute("student", info);
		return "/student/student_update";
	}
	
	
	/**
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/udpate",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	public Results udpate(@RequestBody TTudentInfo  info,Model model,HttpServletRequest request) {
		sTudentService.updateStudent(info);
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "修改成功！");
		return results;
	}

	@ResponseBody
	@RequestMapping(value = "/add",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	public Results add(@RequestBody TTudentInfo info,HttpServletRequest request) {
		info.setStatus(1);
		info.setAvailable(1);
		info.setCreateTime(new Date());
		sTudentService.save(info);
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "保存成功！");
		return results;
	}
	
	@ResponseBody
	@RequestMapping(value = "/save",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	public Results save(@RequestBody TTudentInfo info,HttpServletRequest request) {
		TTudentInfo currentInfo = sTudentService.getInfo(info.getId());
		
		currentInfo.setUpdateTime(new Date());
		currentInfo.setName(info.getName());
		currentInfo.setParentName(info.getParentName());
		currentInfo.setPhone(info.getPhone());
		currentInfo.setEmail(info.getEmail());
		currentInfo.setSex(info.getSex());
		currentInfo.setClassId(info.getClassId());
		currentInfo.setSchoolId(info.getSchoolId());
		currentInfo.setGradeId(info.getGradeId());
		
		System.out.println("===="+info.getName());
		sTudentService.update(currentInfo);
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "保存成功！");
		return results;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "examine", produces = "application/json;charset=UTF-8")
	public String examine(HttpServletRequest request,HttpServletResponse response) {
		String id = request.getParameter("stuid");
		System.out.println("id======="+id);
		TTudentInfo info = new TTudentInfo();
		info.setId(Integer.parseInt(id));
		info.setStatus(1);
		sTudentService.update(info);
		Map<String, Object> resulMmap = new HashMap<String, Object>();
		resulMmap.put("result", "success");
		return JSON.toJSONString(resulMmap);
	}
}
